package cn.com.bluemoon.daps.sync.core.sync.compare.db2model;

import cn.com.bluemoon.daps.api.model.RemoteDataModelTableService;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.sync.base.DevModelCompareResult;
import cn.com.bluemoon.daps.sync.core.sync.GlobalConfig;
import cn.com.bluemoon.daps.sync.core.sync.Tuple2;
import cn.com.bluemoon.daps.sync.core.sync.compare.TableComparator;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author Jarod.Kong
 * @date 2021/6/3 13:52
 */
@Slf4j
public class Db2ModelTableComparator extends TableComparator<DevModelCompareResult> {
    private final RemoteDataModelTableService tableService;

    public Db2ModelTableComparator(GlobalConfig globalConfig, Tuple2<DapDataModelTable> tableTuple2) {
        super(globalConfig, tableTuple2);
        tableService = SpringUtil.getBean(RemoteDataModelTableService.class);
        if (globalConfig.isAddSink()) {
            BmAssetUtils.notNull(tableService, "入库DB，服务类不可为空");
        }
    }


    @Override
    public void change0(List<DevModelCompareResult> collector) {
        super.change0(collector);
        collector.add(DevModelCompareResult.builder()
                .action(DevModelCompareResult.Action.Update)
                .type(DevModelCompareResult.Type.Table)
                .devProp("")
                .modelProp("")
                .build());
        if (globalConfig.isAddSink()) {
            ResultBean<Boolean> res = tableService.updateById(getTableId(), newTable);
            BmAssetUtils.isTrue(res.isOk(), res.getMsg());
            log.info("更新表{}", getTableId());
        }

    }

    @Override
    public void drop0(List<DevModelCompareResult> collector) {
        DevModelCompareResult.Action action = DevModelCompareResult.Action.Delete;
        if (!globalConfig.isCanDropDbTable() && oldTable != null && newTable == null) {
            action = DevModelCompareResult.Action.Ignore;
            log.debug("Dev2ModelTableComparator#drop0，表级别中，库表无，模型有，忽略，不进行入库");
        }
        super.drop0(collector);
        collector.add(DevModelCompareResult.builder()
                .action(action)
                .type(DevModelCompareResult.Type.Table)
                .devProp("")
                .modelProp("")
                .build());
        // 2021/6/16 开发库同步到模型 逻辑调整：开发库表中无，模型中有的表级别，不对模型中的表删除，字段级别对比则进行删除
        if (globalConfig.isAddSink() && !DevModelCompareResult.Action.Ignore.equals(action)) {
            ResultBean<Boolean> res = tableService.removeById(getTableId());
            BmAssetUtils.isTrue(res.isOk(), res.getMsg());
            log.info("删除表{}", getTableId());
        }
    }

    @Override
    public void create0(List<DevModelCompareResult> collector) {
        super.create0(collector);
        collector.add(DevModelCompareResult.builder()
                .action(DevModelCompareResult.Action.Create)
                .type(DevModelCompareResult.Type.Table)
                .devProp("")
                .modelProp("")
                .build());
        if (globalConfig.isAddSink()) {
            newTable.setGroupPid(globalConfig.getDefaultGroup().getPid());
            newTable.setGroupId(globalConfig.getDefaultGroup().getId());
            newTable.setDataModelId(globalConfig.getModel().getId());
            newTable.setDataSourceId(globalConfig.getDataSource().getId());
            newTable.setPId("0");
            newTable.setDataNewest(globalConfig.isModelNewest() ? 1 : 0);
            ResultBean<String> res = tableService.saveTable(newTable);
            BmAssetUtils.isTrue(res.isOk(), res.getMsg());
            String tableId = res.getContent();
            getNewT().setId(tableId);
            newTable.setId(tableId);
            log.info("写入表{}情况：{}", newTable.getEngName(), res.isOk());
        }
    }

}
